package com.it2297.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

import com.it2297.controller.CompanyController;
import com.it2297.main.CompanyApp;


public class CompanyCreateCompany extends JFrame {

	private JMenuBar menuBar;
	private JMenu menuHome;
	private JMenu menuExit;
	private JMenu menuJobAd;
	private JMenuItem menuRemoveJob;
	private JMenuItem menuCreateJob;
	private JMenuItem menuViewAllJobsByCompany;
	private JMenu menuApplicants;
	private JMenuItem menuViewApplicants;
	private JSeparator separator;
	private Component horizontalGlue;
	private Component horizontalStrut;
	private JLabel lblWelcome;
	private JMenuItem menuLogout;
	private JMenuItem menuhomescreen;
	private JMenu mnProfile;
    private JMenuItem mntmUpdateCompany;
	
	
	 private JPanel contentPane;
	 private JTextField tb_name;
     private JTextField tb_email;
     private JTextField tb_phone;
     private JPasswordField pwd;
     private JTextField tf_rqn;
     private JTextField tf_rans;
     private JTextArea tb_address;
     private JTextArea tb_desc;
     private JButton btnSubmit;

     public String getName(){
         return tb_name.getText();
	 }
	
	 public String getPassword(){
	         String password=""; 
	         for(int i=0;i<pwd.getPassword().length;i++){ 
	                 password +=pwd.getPassword()[i];
	         }                
	         return password; 
	 }
	
	 public String getEmail(){
	         return tb_email.getText();
	 }
	 
	 public String getAddress(){
	         return tb_address.getText();
	 }
	
	 public int getPhone(){
	         int phonenum = Integer.parseInt(tb_phone.getText());
	         return phonenum;
	 }
	 public String getDescription(){
	         return tb_desc.getText();
	 }
	 public String getRecoveryQuestion(){
	 	return tf_rqn.getText();
	 }
	 public String getRecoveryAnswer(){
	 	return tf_rans.getText();
	 }

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CompanyCreateCompany frame = new CompanyCreateCompany();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 */
	public CompanyCreateCompany() {
		setTitle("Company - Company Registration");
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 740, 560);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		getContentPane().setLayout(null);
		setLocationRelativeTo(null);
        contentPane.setLayout(null);

        JLabel lblCompanyRegistration = new JLabel("Company Registration");
        lblCompanyRegistration.setFont(new Font("Times New Roman", Font.BOLD, 35));
        lblCompanyRegistration.setBounds(10, 11, 679, 55);
        getContentPane().add(lblCompanyRegistration);

        JLabel lblCompanyName = new JLabel("Company Name:");
        lblCompanyName.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblCompanyName.setBounds(10, 62, 188, 24);
        getContentPane().add(lblCompanyName);

        JLabel lblNewLabel = new JLabel("Company Email:");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel.setBounds(10, 336, 188, 24);
        getContentPane().add(lblNewLabel);              

        JLabel lblNewLabel_1 = new JLabel("Company Address:");
        lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel_1.setBounds(10, 97, 188, 24);
        getContentPane().add(lblNewLabel_1);               

        JLabel lblNewLabel_2 = new JLabel("Company Phone Number:");
        lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel_2.setBounds(10, 301, 188, 24); 
        getContentPane().add(lblNewLabel_2);

        JLabel lblNewLabel_3 = new JLabel("Company Description:");
        lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 14)); 
        lblNewLabel_3.setBounds(10, 187, 188, 24); 
        getContentPane().add(lblNewLabel_3);                 

        tb_name = new JTextField(); 
        tb_name.setBounds(208, 63, 361, 26); 
        getContentPane().add(tb_name); 
        tb_name.setColumns(10);               

        tb_email = new JTextField(); 
        tb_email.setBounds(208, 337, 296, 26); 
        getContentPane().add(tb_email); 
        tb_email.setColumns(10); 

        tb_phone = new JTextField(); 
        tb_phone.setBounds(208, 300, 130, 26); 
        getContentPane().add(tb_phone);
        tb_phone.setColumns(10);

        btnSubmit = new JButton("Submit");
        btnSubmit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) { 
                				try{
                                      String password="";
                                        for(int i=0;i<pwd.getPassword().length;i++){
                                                password +=pwd.getPassword()[i];
                                        }                                               
                                        if(tb_name.getText().equals("") || password.equals("") ||tb_email.getText().equals("") || tb_phone.getText().equals("") || tb_address.getText().equals("")||tb_desc.getText().equals("")||tf_rqn.getText().equals("")||tf_rans.getText().equals("")){
                                                JOptionPane.showMessageDialog(null,"Please fill in all the necessary details.");
                                        }
                                        else{
                                        	if(tb_phone.getText().length()>8){
                                        		JOptionPane.showMessageDialog(null, "Phone number has more than 8 digits!");
                                        	}
                                        	else{
                                        		createCompany();
                                        	}
                                        } 
                                } 
                                catch(NumberFormatException e){
                                        JOptionPane.showMessageDialog(null,"Please provide a proper phone number.");
                                }                    
                        }
                });
        btnSubmit.setBounds(208, 485, 109, 26);
        btnSubmit.setFont(new Font("Times New Roman", Font.BOLD, 18));
        getContentPane().add(btnSubmit);
   
        pwd = new JPasswordField();
        pwd.setBounds(208, 374, 235, 26); 
        getContentPane().add(pwd);
      
        JLabel lblPwd = new JLabel("Password:");
        lblPwd.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblPwd.setBounds(10, 373, 188, 24);
        getContentPane().add(lblPwd);    
        
        JLabel lblRecoveryQuestion = new JLabel("Recovery Question:");
        lblRecoveryQuestion.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblRecoveryQuestion.setBounds(10, 412, 188, 24);
        getContentPane().add(lblRecoveryQuestion);
        
        JLabel lblRecoveryAnswer = new JLabel("Recovery Answer:");
        lblRecoveryAnswer.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblRecoveryAnswer.setBounds(10, 447, 188, 24);
        getContentPane().add(lblRecoveryAnswer);
        
        tf_rqn = new JTextField();
        tf_rqn.setBounds(208, 411, 455, 26);
        getContentPane().add(tf_rqn);
        tf_rqn.setColumns(10);
        
        tf_rans = new JTextField();
        tf_rans.setBounds(208, 448, 403, 26);
        getContentPane().add(tf_rans);
        tf_rans.setColumns(10);
        
        JButton btnReset = new JButton("Reset");
        btnReset.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		  tb_name.setText("");
                  pwd.setText("");
                  tb_email.setText("");
                  tb_phone.setText("");
                  tb_address.setText("");
                  tb_desc.setText("");
                  tf_rqn.setText("");
                  tf_rans.setText("");
        	}
        });
        btnReset.setFont(new Font("Times New Roman", Font.BOLD, 18));
        btnReset.setBounds(327, 485, 109, 26);
        getContentPane().add(btnReset);
        
        tb_address = new JTextArea();
        tb_address.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
        tb_address.setBounds(208, 100, 277, 78);
        getContentPane().add(tb_address);
        
        tb_desc = new JTextArea();
        tb_desc.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
        tb_desc.setBounds(208, 189, 487, 100);
        getContentPane().add(tb_desc);
        
        
        
        
        menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 724, 24);
		getContentPane().add(menuBar);
		
		menuHome = new JMenu("Home");
		menuBar.add(menuHome);
		menuhomescreen=new JMenuItem("Home Screen");
		menuhomescreen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		menuHome.add(menuhomescreen);
		
		menuJobAd=new JMenu("Job Advertisement");
		menuBar.add(menuJobAd);
		menuCreateJob=new JMenuItem("Create Job Advertisement");
		menuCreateJob.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JobCreateJob jcj=new JobCreateJob();
				jcj.setLocationRelativeTo(null);
				jcj.setVisible(true);
				closeWindow();
			}
		});
		menuJobAd.add(menuCreateJob);
		
		JSeparator separator_1 = new JSeparator();
		menuJobAd.add(separator_1);
		menuRemoveJob=new JMenuItem("Remove Job Advertisement");
		menuJobAd.add(menuRemoveJob);
		
		JSeparator separator_2 = new JSeparator();
		menuJobAd.add(separator_2);
		menuViewAllJobsByCompany=new JMenuItem("View All Jobs By Company");
		menuViewAllJobsByCompany.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CompanyViewAllJobsByCompany cvajbc=new CompanyViewAllJobsByCompany();
				cvajbc.setLocationRelativeTo(null);
				cvajbc.setVisible(true);
				cvajbc.setTitle("Company - View All Jobs By Company ");
				closeWindow();
			}
		});
		menuJobAd.add(menuViewAllJobsByCompany);
			
		horizontalGlue = Box.createHorizontalGlue();
		menuBar.add(horizontalGlue);
		
		
		
		lblWelcome = new JLabel();
		lblWelcome.setForeground(new Color(0, 0, 128));
		if (CompanyApp.isUserLogin())
		{
			lblWelcome.setText("Welcome, " + CompanyApp.getCurrentUser().getCompanyName() + ".");
		}
		else
		{
			lblWelcome.setText("Welcome! Please login to manage your job advertisements.");
		}
		menuBar.add(lblWelcome);
		
		horizontalStrut = Box.createHorizontalStrut(20);
		menuBar.add(horizontalStrut);
		
		
		
		mnProfile = new JMenu("Profile");
		menuBar.add(mnProfile);
		
		mntmUpdateCompany = new JMenuItem("Update Company");
		mntmUpdateCompany.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CompanyUpdateCompany cuc = new CompanyUpdateCompany();
				cuc.setVisible(true);
				cuc.setTitle("Update Company Details");
				closeWindow();
			}
		});
		mnProfile.add(mntmUpdateCompany);
		
		menuExit = new JMenu("Exit");
		menuBar.add(menuExit);
		menuLogout=new JMenuItem("Back to Company Login");
		menuLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeWindow();
			}
		});
		menuExit.add(menuLogout);
		
		
		
		if (CompanyApp.isUserLogin())
		{
			 menuHome.setEnabled(true);
			 menuJobAd.setEnabled(true);
			 mnProfile.setEnabled(true);
		}
		else
		{
			 menuHome.setEnabled(false);
			 menuJobAd.setEnabled(false);
			 mnProfile.setEnabled(false);
		}
	}

		private void createCompany(){
		        CompanyController pc = new CompanyController(this);
		        pc.CreateCompany();
		}
		
		
		public void closeWindow(){
			this.dispose();
		}
}
